/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.rails.extjs.data;

import java.util.ArrayList;
import java.util.List;

import org.rails.extjs.Observable;
import org.rails.extjs.Option;
import org.rails.extjs.Parameters;
import org.rails.extjs.json.Json;

public abstract class Store extends Observable {
	
	protected final List<Field> fields = new ArrayList<Field>();
	
	public Store(){
		super();	
		setStoreId(createId());
	}
	
	public Store(String storeId){
		super();	
		setStoreId(storeId);
	}
	
	public Store(DataProxy proxy){
		super();
		setProxy(proxy);
		setStoreId(createId());
	}
	
	public void setReader(DataReader reader){
		addOption("reader",reader);
	}
	
	public DataReader getReader(){
		return (DataReader)getOption("reader");
	}
	
	public void addField(Field field){
		fields.add(field);
	}
	
	public void getField(int index){
		fields.get(index);
	}
	
	public void removeField(int index){
		fields.remove(index);
	}
	
	public void clearField(){
		fields.clear();
	}
	
	public List<Field> getFields(){
		return fields;
	}
	
	public void setAutoDestroy(Boolean autoDestroy){
		addOption("autoDestroy", autoDestroy);
	}
	
	public Boolean isAutoDestroy(){
		return getBoolean("autoDestroy", false);
	}
	
	public void setAutoLoad(Boolean autoLoad){
		addOption("autoLoad", autoLoad);
	}
	
	public Boolean isAutoLoad(){
		return getBoolean("autoLoad",true);
	}
	
	public void setAutoSave(Boolean autoSave){
		addOption("autoSave", autoSave);
	}
	
	public Boolean isAutoSave(){
		return getBoolean("autoSave", true);
	}
	
	public void setBaseParams(Parameters baseParams){
		addOption("baseParams",baseParams);
	}
	
	public Parameters getBaseParams(Parameters baseParams){
		return (Parameters)getOption("baseParams");
	}
	
	public void setBatch(Boolean batch){
		addOption("batch", batch);
	}
	
	public Boolean isBatch(){
		return getBoolean("batch", true);
	}
	
	public void setData(Json data){
		addOption("data",data);
	}
	
	public Json getData(){
		return (Json)getOption("data");
	}
	
	public void setDefaultParamNames(Option defaultParamNames){
		addOption("defaultParamNames", defaultParamNames);
	}
	
	public Option getDefaultParamNames(){
		return (Option)getOption("defaultParamNames");
	}
	
	public void setParamNames(Option paramNames){
		addOption("paramNames",paramNames);
	}
	
	public Option getParamNames(){
		return (Option)getOption("paramNames");
	}
	
	public void setProxy(DataProxy proxy){
		addOption("proxy", proxy);
	}
	
	public DataProxy getProxy(DataProxy proxy){
		return (DataProxy)getOption("proxy");
	}
	
	public void setPruneModifiedRecords(Boolean pruneModifiedRecords){
		addOption("pruneModifiedRecords", pruneModifiedRecords);
	}
	
	public Boolean isPruneModifiedRecords(){
		return getBoolean("pruneModifiedRecords",false);
	}
	
	public void setRemoteSort(Boolean remoteSort){
		addOption("remoteSort", remoteSort);
	}
	
	public Boolean isRemoteSort(){
		return getBoolean("remoteSort", false);
	}
	
	public void setSortInfo(String field,String direction){
		Option sortInfo = new Option();
		sortInfo.put("field",field);
		sortInfo.put("direction", direction);
		addOption("sortInfo",sortInfo);
	}
	
	public void setStoreId(String storeId){
		addOption("storeId", storeId);
	}
	
	public String getStoreId(){
		return getString("storeId", "");
	}
	
	public void setRoot(String root){
		addOption("root", root);
	}
	
	public String getRoot(){
		return getString("root","");
	}
	
	@Override
	public void createItems(){
		super.createItems();
		if(fields.size() > 0)
			addOption("fields",fields);
	}
	
}

